package com.ruoyi.nsh.util;


import com.ruoyi.common.utils.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * <DL>
 * <DT><B> 日期处理工具 </B></DT>
 * <p>
 * <DD> 详细介绍 </DD>
 * </DL>
 * <p>
 *
 * <DL>
 * <DT><B>使用范例</B></DT>
 * <p>
 * <DD> 使用范例说明 </DD>
 * </DL>
 * <p>
 *
 */
public class DateUtil {

	public synchronized static String getDateString(String formate,Date date) throws Exception{
		SimpleDateFormat sdf=new SimpleDateFormat(formate);
		return sdf.format(date);
	}
	private static long millisecondsOfOneDay = 1000 * 60 * 60 * 24;

	private static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyyMMdd");

	private synchronized static String add(String dateString, int field, int amount) {
		Date date;
		try {
			date = dateFormat.parse(dateString);
		} catch (ParseException e) {
			throw new IllegalArgumentException("0007 八位日期格式错误:" + dateString);
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, amount);
		return dateFormat.format(cal.getTime());
	}

	/**
	 * 在当前日期的基础上增加指定天数
	 *
	 * @param dateString
	 *            八位日期字符串
	 * @param days
	 *            增加的天数, 可以是负数
	 * @return 八位结果日期字符串
	 */
	public synchronized static String addDay(String dateString, int days) {
		return add(dateString, Calendar.DAY_OF_MONTH, days);
	}

	/**
	 * 判断当天日期是否当月最后一天
	 *
	 * @param dateString
	 *            八位日期字符串
	 * @return boolean
	 */
	public synchronized static boolean isLastDay(String dateString) {
		int year = Integer.valueOf(dateString.substring(0,4));
		int month = Integer.valueOf(dateString.substring(4,6));
		String lastday = getLastDayOfMonth(year,month);
		if(dateString.equals(lastday)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 获取某月的最后一天
	 *
	 */
	public static String getLastDayOfMonth(int year,int month)
	{
		Calendar cal = Calendar.getInstance();
		//设置年份
		cal.set(Calendar.YEAR,year);
		//设置月份
		cal.set(Calendar.MONTH, month);
		//获取当月最小值
		int lastDay = cal.getMinimum(Calendar.DAY_OF_MONTH);
		//设置日历中的月份，当月+1月-1天=当月最后一天
		cal.set(Calendar.DAY_OF_MONTH, lastDay-1);
		//格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String lastDayOfMonth = sdf.format(cal.getTime());
		return lastDayOfMonth;
	}

	/**
	 * @param dateString
	 *            八位日期字符串
	 * @param months
	 *            增加的月份数, 可以是负数
	 * @return 八位结果日期字符串
	 */
	public synchronized static String addMonth(String dateString, int months) {
		return add(dateString, Calendar.MONTH, months);
	}

	/**
	 * 在当前日期基础上增加指定月数
	 *
	 * @param dateString
	 *            八位日期字符串
	 * @param years
	 *            增加的年数, 可以是负数
	 * @return 八位结果日期字符串
	 */
	public  synchronized static String addYear(String dateString, int years) {
		return add(dateString, Calendar.YEAR, years);
	}

	/**
	 * <DL>
	 * <DT><B> 计算两个日期之间的天数 </B></DT>
	 * <p>
	 * <DD> start 经过几天到达end </DD>
	 * </DL>
	 * <p>
	 *
	 * @param start
	 *            起始日期
	 * @param end
	 *            截止日期
	 * @return 差异天数.以整天数计算，不考虑小于“天”的时间差异
	 */
	public synchronized static int daysBetween(Date start, Date end) {
		assert (start != null);
		assert (end != null);

		return (int) (end.getTime() / millisecondsOfOneDay - start.getTime()
				/ millisecondsOfOneDay);
	}

	/**
	 * <DL>
	 * <DT><B> 计算两个日期之间的天数 </B></DT>
	 * <p>
	 * <DD> start 经过几天到达end </DD>
	 * </DL>
	 * <p>
	 *
	 * @param startStr
	 *            起始日期
	 * @param startFormatPattern
	 *            时间格式
	 * @param endStr
	 *            截止日期
	 * @param endFormatPattern
	 *            时间格式
	 * @return 差异天数
	 */
	public synchronized static int daysBetween(String startStr, String startFormatPattern,
											   String endStr, String endFormatPattern) throws ParseException {
		return daysBetween(StringUtil.toDate(startStr, startFormatPattern),
				StringUtil.toDate(endStr, endFormatPattern));
	}

	/**
	 * 返回两个时间的差数 。
	 *
	 * @param date1
	 *           要比较的时间
	 * @param date2
	 *           参考时间
	 * @return result 相差的小时时间
	 */
	public synchronized static int getBetweenSeconds(Date date1, Date date2) {
		if (date1 == null || date2 == null)
			return 0;
		Calendar cal1 = new GregorianCalendar(Locale.CHINA);
		Calendar cal2 = new GregorianCalendar(Locale.CHINA);
		cal1.setTime(date1);
		cal2.setTime(date2);

		long timeMillis1 = cal1.getTimeInMillis();
		long timeMillis2 = cal2.getTimeInMillis();
		long result = (timeMillis2 - timeMillis1) / (1000);
		return (int) result;
	}

	public synchronized static Date getDateByFormat(String format,String dateStr){
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		try {
			return sdf.parse(dateStr);
		} catch (Exception e) {
		}
		return null;
	}

	public synchronized static String getDateByFormat(String targetFormat,String sourceFormat,String dateStr){
		SimpleDateFormat ssdf=new SimpleDateFormat(sourceFormat);
		try {
			SimpleDateFormat tsdf=new SimpleDateFormat(targetFormat);
			return tsdf.format(ssdf.parse(dateStr));
		} catch (Exception e) {
		}
		return null;
	}

	public static void main(String[] args) throws Exception{
		//20160414	20170412	20160414
		/*System.out.println(daysBetween("20170326", "yyyyMMdd", "20170328", "yyyyMMdd"));

		String kjrq = "";
		String todayStr = "";
		String ncrq="";
		try {
			todayStr = DateUtil.getDateString("yyyyMMdd", new Date());
			kjrq = DateUtil.addDay(todayStr, -1);
			ncrq=DateUtil.addDay(kjrq.substring(0, 4)+"0101",-1);
			System.out.println(ncrq);
		} catch (Exception e) {
			// 不会发生的异常
		}
*/

		String tttt = DateUtils.getdateYYYYMMDD();
		System.out.println( isLastDay(tttt)  );
		System.out.println( isLastDay("20220229" )  );

	}
}


